<template>
  <div class="xtx-pay-page">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/cart">购物车</XtxBreadItem>
        <XtxBreadItem>支付订单</XtxBreadItem>
      </XtxBread>
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{ countTimeStr }}</span>, 超时后将取消订单</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ orderInfo.payMoney?.toFixed(2) }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <a class="btn alipay" :href="payUrl"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getOrderInfoAPI } from '@/api/order'
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useIntervalFn } from '@vueuse/core'
import { message } from '@/components/Message'
export default {
  name: 'XtxPayPage',
  setup () {
    const route = useRoute()
    const router = useRouter()
    // 订单详情
    const orderInfo = ref({})
    const countTime = ref(60)
    getOrderInfo()
    // 获取订单详情
    async function getOrderInfo () {
      // 处理订单超时
      if (countTime.value === -1) {
        message({ type: 'error', text: '订单已超时' })
        // router.push('/settlement')
        // 回到上个路由
        router.go(-1)
        return
      }
      // 1. 发请求、根据id获取订单详情
      const resp = await getOrderInfoAPI(route.params.orderId)
      // 2. 赋值
      orderInfo.value = resp.result
      // 3. 保存倒计时的时间
      countTime.value = resp.result.countdown
    }
    const { pause } = useIntervalFn(() => {
      if (countTime.value === -1) {
        pause()
        return
      }
      countTime.value--
    }, 1000)

    // 倒计时的字符串
    const countTimeStr = computed(() => {
      // 基于countTime数字转成xxx分xxx秒
      // 1. 得到分
      let m = parseInt(countTime.value / 60)
      // 2. 得到秒
      let s = countTime.value % 60
      // 补零
      m = m >= 10 ? m : '0' + m
      s = s >= 10 ? s : '0' + s
      return `${m}分${s}秒`
    })

    // 支付相关的地址
    // const payUrl = '后台服务基准地址+支付页面地址+订单ID+回跳地址'
    const baseURL = 'http://pcapi-xiaotuxian-front-devtest.itheima.net/'
    const redirectUrl = encodeURIComponent('http://www.corho.com:8080/#/pay/callback')
    const payUrl = `${baseURL}pay/aliPay?orderId=${route.params.orderId}&redirect=${redirectUrl}`

    return {
      orderInfo,
      countTimeStr,
      payUrl
    }
  }
}
</script>
<style scoped lang="less">
.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: @xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }
  }
}
</style>
